Skip to content

Design-1 solutions python - minstack, hashset#2637

Open
shaikhmisrail wants to merge 1 commit intosuper30admin:masterfrom
shaikhmisrail:master
Open

Design-1 solutions python - minstack, hashset#2637
shaikhmisrail wants to merge 1 commit intosuper30admin:masterfrom
shaikhmisrail:master

Conversation

@shaikhmisrail
Copy link

No description provided.

@super30admin
Copy link
Owner

Strengths:

  • The student has attempted to use double hashing, which is a good approach for this problem.
  • The code includes comments and docstrings, which is good practice.

Areas for improvement:

  1. Primary and Secondary Hashing: The primary hash function should use modulo to determine the bucket index, and the secondary hash function should use integer division to determine the index within the bucket. This is the opposite of what you have implemented. For example:

    • Primary hash: key % primaryBuckets
    • Secondary hash: key // primaryBuckets (or key // secondaryBuckets as in the reference)
  2. Initialization of Storage: The self.hashset should be initialized as an array of size primaryBuckets (e.g., 1000) with None values. Then, when adding a key, you check if the primary bucket is None and initialize the secondary array accordingly. In your code, you initialized it as a list of booleans, which is incorrect.

  3. Handling the Last Bucket: To handle key=1000000, the reference solution uses a primary bucket size of 1000, and for primary index 0, it creates a secondary array of size 1001. This is because 1000000 % 1000 = 0, and 1000000 // 1000 = 1000. So without the extra slot, the secondary index would be out of bounds. Your solution uses primary bucket size 1001, but the hashing functions are not aligned.

  4. Type Consistency: The self.hashset array should store either None or lists (secondary arrays). You should not store booleans directly in the primary array (except for the special case in the reference, which is handled by having a larger secondary array for one bucket).

  5. Edge Cases: Consider what happens when you add key=0, key=1000, key=1000000. Your current implementation will not handle these correctly.

Suggested corrections:

  • Change the primary bucket size to 1000 (not 1001) for the primary array.
  • Initialize self.hashset = [None] * primaryBuckets
  • For primary hash: key % primaryBuckets
  • For secondary hash: key // primaryBuckets
  • When initializing a secondary array, for primary index 0, create an array of size 1001 (to accommodate secondary indices from 0 to 1000), and for other indices, create an array of size 1000.

Alternatively, you can use a primary array of size 1001 and secondary arrays of size 1000, but then you need to adjust the hashing to avoid index out of bounds for key=1000000. The reference solution does it with primary size 1000 and a special case for index 0.

@super30admin
Copy link
Owner

Strengths:

  • You have attempted to use the double hashing technique, which is a good approach for this problem.
  • You have provided docstrings and comments, which is helpful for understanding the code.
  • You have considered the edge case for the last bucket (index1000).

Areas for improvement:

  1. Data Structure: Instead of initializing self.hashset as a list of booleans, it should be a list that can hold either None (or False) or a list (secondary array). In your code, you are trying to assign a list to self.hashset[index1] when it is False, but since self.hashset is initialized as a list of booleans, this will cause a type error. You should initialize self.hashset as a list of None values of length primaryBucket+1 (to include index1000).

  2. Hash Functions: The primary hash function should be modulo 1000 to distribute keys evenly. Your primary hash function uses integer division by 1000, which is not standard. For example, key=1 would give index1=0, key=1000 would give index1=1, but key=1000000 would give index1=1000. The reference solution uses modulo for the primary hash and integer division for the secondary hash. This ensures that for a key k, the primary index is k % 1000 and the secondary index is k / 1000. This way, key=1000000 has primary index0 and secondary index1000.

  3. Handling the First Bucket: The first bucket (index0) should have a secondary array of size 1001 to accommodate key=1000000. In your code, when you create the secondary array for a primary index, you need to check if it is index0 and allocate an array of size 1001, otherwise size 1000.

  4. Type Consistency: When you check if a primary bucket is initialized, you are checking if not self.hashset[index1], which might work if you initialize with None, but not with False. You should initialize the primary array with None and then set it to a list when needed.

  5. Removal and Containment: In the remove method, you are setting the value to False, which is correct, but you need to ensure that the secondary array exists before accessing it. Similarly, for contains, you need to check if the secondary array exists.

  6. Edge Case for Key=1000000: In your current code, for key=1000000, you are setting self.hashset[1000] = True. This is incorrect because the primary array should have a secondary array for each primary index. Instead, for key=1000000, the primary index should be 0 (if using modulo) and secondary index1000. So you should not have a special case for index1000 in the primary array.

Suggested corrections:

  • Change the primary hash function to key % self.primaryBucket.
  • Change the secondary hash function to key // self.secondaryBucket.
  • Initialize self.hashset as [None] * (self.primaryBucket) for indices 0 to 999, but you need to handle index1000? Actually, since primaryBucket is 1000, the modulo operation gives indices from 0 to 999. However, key=1000000 would be stored at primary index0 and secondary index1000. So the primary array should be of size 1000, and for the first bucket, the secondary array should be of size 1001.

Alternatively, you can follow the reference solution exactly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments